<?php

namespace app\index\controller;

use think\db\Where;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
use utilTool\Random;

class IMine extends Base
{

    # 获取用户信息
    public function getUserLists()
    {
        $uid =   trim(input('post.uid'));
        $data = Db::table('app_user')->where('uid', $uid)->find();
        // 删除不必要的数据
        unset($data['upwd']);
        unset($data['salt']);
        $this->returnCode(0, $data);
    }


    # 注册
    public function register()
    {

        // 默认使用 ' a+手机 ' 号作为账号名称
        $data['uaccount'] = 'a' . trim(input('post.phone'));
        $data['phone'] = trim(input('post.phone'));
        // 生成随机数密码盐
        $data['salt'] = Random::build('alnum', 16);
        // 双重加密
        $data['upwd'] = md5(md5(trim(input('post.password'))) . $data['salt']);
        $data['joinip'] = get_client_ip();
        $data['jointime'] =  time();
        $data['avatar'] = '/static/logo.png';
        $data['balance'] = 0;
        $res = Db::table('app_user')->insert($data);
        if (!$res) {
            $this->returnCode(81000002);
        }
        $this->returnCode(0, '注册成功');
    }

    # 登录
    public function login()
    {
        $phone = trim(input('post.phone'));
        $upwd = trim(input('post.password'));

        $res = Db::table('app_user')->where('phone', $phone)->find();
        // 验证手机号是否存在
        if (empty($res)) {
            $this->returnCode(81000003);
        }
        // 判读密码是否正确
        if ($res['upwd'] != md5(md5($upwd) . $res['salt'])) {
            $this->returnCode(81000004);
        }

        // 判读用户是否被拉黑
        if ($res['status'] != 1) {
            $this->returnCode(81000005);
        }

        Db::table('app_user')->where('uid', $res['uid'])->update(['lastloginip' => get_client_ip(), 'lastlogintiem' => time()]);
        $this->returnCode(0, ['uid' => $res['uid'], 'uaccout' => $res['uaccount']]);
    }

    # 根据用户ID获取用户数据
    public function getUserById()
    {
        $uid =   trim(input('post.uid'));
        $data = Db::table('app_user')->where('uid', $uid)->find();
        // 删除不必要的数据
        unset($data['upwd']);
        unset($data['salt']);
        $this->returnCode(0, $data);
    }
    # 根据用户ID更新用户数据
    public function updateUserById()
    {
        $uid =   trim(input('post.uid'));
        $data = input("post.data");
        // 判断性别
        switch ($data['gender']) {
            case '男':
                $data['gender'] = 1;
                break;
            case '女':
                $data['gender'] = 2;
                break;
            default:
                $data['gender'] = 0;
                break;
        }
        $res = Db::table('app_user')->where('uid', $uid)->update($data);
        if (!$res) {
            $this->returnCode(81000006);
        }
        $data = Db::table('app_user')->where('uid', $uid)->find();
        // 删除不必要的数据
        unset($data['upwd']);
        unset($data['salt']);
        $this->returnCode(0, $data);
    }

    # 查询用户余额
    public function getBalance()
    {
        $uid =   trim(input('post.uid'));
        $res = Db::table('app_user')->where('uid', $uid)->find();
        if (!$res) {
            $this->returnCode(81000000);
        }
        $this->returnCode(0, $res['balance']);
    }

    # 查询默认收货地址
    public function getDefaultAddr()
    {
        $uid =   trim(input('post.uid'));
        $aid = Db::table('app_user')->where('uid', $uid)->value('a_id');
        if (!$aid) {
            $this->returnCode(81000013);
        }
        $aids = explode(',', $aid);
        for ($i = 0; $i < count($aids); $i++) {
            $res = Db::table('app_user_address')->where(['a_id' => $aids[$i], 'default' => 1])->find();
        }
        if (!$res) {
            $this->returnCode(81000012);
        }
        $this->returnCode(0, $res);
    }

    # 获取该用户下所有收货地址
    public function getAddressAll()
    {
        $uid = input('post.uid');
        $aid = Db::table('app_user')->where('uid', $uid)->value('a_id');
        // $aid = $user['a_id'];
        $aids = explode(',', $aid);
        for ($i = 0; $i < count($aids); $i++) {
            $res[$i] = Db::table('app_user_address')->where('a_id', $aids[$i])->find();
        }
        $this->returnCode(0, $res);
    }

    # 更新或新增收货地址
    public function addressSave()
    {
        $uid = input('post.uid');
        $data = input('post.data');
        $data['address'] = $data['address'] . ' ' . $data['detailAddr'];
        $data['default'] = $data['default'] == true ? 1 : 0;
        unset($data['detailAddr']);

        if ($data['a_id'] == 0) {
            unset($data['a_id']);
            $aid = Db::table('app_user_address')->insertGetId($data);
            if (!$aid) {
                $this->returnCode(81000011);
            }
            // 将收货地址绑定给用户
            $before_aid = Db::table('app_user')->where('uid', $uid)->value('a_id');
            $aid = $before_aid != '' ? $before_aid . ',' . $aid : $aid;
            $res = Db::table('app_user')->where('uid', $uid)->update(['a_id' => $aid]);
            if (!$res) {
                $this->returnCode(81000011);
            }
            $this->returnCode(0);
        } else {
            $this->returnCode(0, $data);
        }
    }

    # 获取该用户下所有的订单
    public function getOrderAll()
    {
        $uid = trim(input('post.uid'));
        $status =  trim(input('post.status'));
        $result = '';

        switch ($status) {
            case 0:
                $result = Db::table('app_shop_order')->where('uid', $uid)->select()->toArray();
                break;
            case 1:
                $result = Db::table('app_shop_order')->where(['uid' => $uid, 'status' => 1])->select()->toArray();
                break;
            case 2:
                $result = Db::table('app_shop_order')->where(['uid' => $uid, 'status' => 2])->select()->toArray();
                break;
            case 3:
                $result = Db::table('app_shop_order')->where(['uid' => $uid, 'status' => 3])->select()->toArray();
                break;
            case 4:
                $result = Db::table('app_shop_order')->where(['uid' => $uid, 'status' => 4])->select()->toArray();
                break;
            case 5:
                $result = Db::table('app_shop_order')->where(['uid' => $uid, 'status' => 5])->select()->toArray();
                break;
        }
        if (!$result) {
            $this->returnCode(81000010);
        }

        foreach ($result as $key => &$item) {
            $item["time"] = date('Y年m月d日 H:m:s', $item['pay_time']);
            $result[$key]['goods'] = Db::table('app_shop_goods_info')->where('gid', $item['gid'])->find();
            // 遍历数组 并将图片路径设置为绝对路径 方便前端展示
            if (!empty($result[$key]['goods']['img'])) {
                // 将字符串以 ；符号分割为数组
                $img =  explode(';', $result[$key]['goods']['img']);
                // 弹出数组中最后一个元素 因为以 ；分隔为数组后最后一个元素为空值
                array_pop($img);
                foreach ($img as $k => $img_v) {
                    if (!empty($img_v)) {
                        $result[$key]['goods']['img_s'][] = [
                            'uid' => $k,
                            'url' => $this->domain_name . $img_v,
                            'data' => $img_v
                        ];
                    }
                }
            }
        }

        $this->returnCode(0, $result);
    }
}
